#include "MLConsoleCommander.h"
#include "MLTools.h"
_MYLABLIB_BEGIN

CMLConsoleCommander::CMLConsoleCommander(void)
{
}

CMLConsoleCommander::~CMLConsoleCommander(void)
{
}

int CMLConsoleCommander::ExecuteCommandFile(const std::wstring& wstrFilename)
{
	int nRet;
	std::wifstream commandFile;
	// Open the file
	commandFile.open(wstrFilename.c_str());
	if (commandFile.fail())
	{
		wprintf(L"Cannot open the command file : %s\n", wstrFilename.c_str());
		ML_ASSERT(false);
		return -1;
	}
	// Read the command lines
	std::wstring wstrCommandLine;
	while(commandFile.eof() == false)
	{
		// Read the line
		std::getline(commandFile, wstrCommandLine);
		// If it is blank line
		if (CMLTools::IsWhitespaceString(wstrCommandLine))
			continue;
		// If it is comment
		if (wstrCommandLine.length() > 0 && wstrCommandLine[0] == L'#')
			continue;
		// Execute this command line
		nRet = Run(wstrCommandLine);
		if (ML_FAILED(nRet))
			break;
	}
	commandFile.close();
	return nRet;
}

int CMLConsoleCommander::Run(const std::wstring& wstrCommand)
{
	std::vector<std::wstring> vCommand;
	mylablib::CMLTools::SplitString(wstrCommand, vCommand);
	int i;
	for (i=0; i<(int)vCommand.size(); i++)
	{
		if (vCommand[i].size() > 0 && vCommand[i][0] == '#')
		{
			vCommand.erase(vCommand.begin()+i);
			i--;
		}
	}
	return Run(vCommand);	
}

int CMLConsoleCommander::Run(const std::vector<std::wstring>& vCommands)
{
	return -1;
}

_MYLABLIB_END
